Moesif AWS Lambda Middleware for Ruby
by Moesif, the API analytics and API monetization platform.
With Moesif Ruby middleware for AWS Lambda, you can automatically log API calls and send them to Moesif for API analytics and monitoring. This middleware allows you to integrate Moesif’s API analytics and API monetization features into your Ruby applications with minimal configuration.
If you’re new to Moesif, see our Getting Started resources to quickly get up and running.
This middleware expects the Lambda proxy integration type. If you’re using AWS Lambda with API Gateway, you are most likely using the proxy integration type.
Prerequisites
Before using this middleware, make sure you have the following:
Get Your Moesif Application ID
After you log into Moesif Portal, you can get your Moesif Application ID during the onboarding steps. You can always access the Application ID any time by following these steps from Moesif Portal after logging in:
- Select the account icon to bring up the settings menu.
- Select Installation or API Keys.
- Copy your Moesif Application ID from the Collector Application ID field.
Install the Middleware
Install with Bundler:
bundle install moesif_aws_lambda
Configure the Middleware
See the available configuration options to learn how to configure the middleware for your use case.
How to use
1. Wrap your Original Lambda handler with Moesif Middleware
def your_original_handler(event:, context:)
# your code
end
moesif_options = {
# application_id is required
"application_id" => 'Your Moesif Application Id',
"debug" => true,
# see list of other options below.
}
# create moesif_middleware object.
$moesif_middleware = Moesif::MoesifAwsMiddleware.new(method(:your_original_handler), moesif_options)
2. Create a Wrapped Handler and Set it to the Function Handler Name
def wrapped_handler(event:, context:)
$moesif_middleware.handle(event: event, context: context);
end
Then set the AWS Lambda handler name to wrapped_handler
instead. For more information, see Define Lambda function handler in Ruby.
Troubleshoot
For a general troubleshooting guide that can help you solve common problems, see Server Troubleshooting Guide.
Other troubleshooting supports:
Configuration Options
The following sections describe the available configuration options for this middleware. You must set these options in a Ruby Hash
object when you create the middleware instance. See the sample Lambda handler function code in example_handler.rb
for better understanding.
application_id
(Required)
Data type |
---|
String |
A string that identifies your application in Moesif.
api_version
Data type |
---|
String |
Optional.
Use it to tag requests with the version of your API.
identify_user
Data type | Return type |
---|---|
Proc
|
String |
Optional, but highly recommended.
A Proc
that takes the event, context, and Lambda result as arguments.
Returns a string that represents the user ID used by your system.
Moesif identifies users automatically. However, due to the differences arising from different frameworks and implementations, set this option to ensure user identification properly.
In the following code snippet, event
and context
are original Lambda handler’s input parameters. result
is the return result from your own original Lambda handler.
moesif_options['identify_user'] = Proc.new { |event, context, result|
# Add your custom code that returns a string for user id
'12345'
}
identify_company
Data type | Return type |
---|---|
Proc
|
String |
Optional.
A Proc
that takes the event, context, and Lambda result as arguments.
Returns a string that represents the company ID for this event. This helps Moesif attribute requests to unique company.
moesif_options['identify_company'] = Proc.new { |event, context, result|
# Add your custom code that returns a string for company id
'67890'
}
identify_session
Data type | Return type |
---|---|
Proc
|
String |
A Proc
that takes env
, headers
, and body
as arguments.
Returns a string that represents the session token for this event.
Similar to users and companies, Moesif tries to retrieve session tokens automatically. But if it doesn’t work for your service, use this option to help identify sessions.
moesif_options['identify_session'] = Proc.new { |event, context, result|
# Add your custom code that returns a string for session/API token
'XXXXXXXXX'
}
get_metadata
Data type | Return type |
---|---|
Proc
|
Hash
|
Optional.
A Proc
that takes env
, headers
, and body
as arguments.
Returns a Hash
that represents a JSON object. This allows you to attach any
metadata to this event.
moesif_options['get_metadata'] = Proc.new { |event, context, result|
# Add your custom code that returns a dictionary
value = {
'datacenter' => 'westus',
'deployment_version' => 'v1.2.3'
}
value
}
mask_data
Data type | Return type |
---|---|
Proc
|
EventModel
|
Optional.
A Proc that takes an EventModel
as an argument and returns an EventModel
.
This option allows you to modify headers or body of an event before sending the event to Moesif.
moesif_options['mask_data'] = Proc.new { |event_model|
# Add your custom code that returns a event_model after modifying any fields
event_model.response.body.password = nil
event_model
}
For more information and the spec of Moesif’s event model, see the source code of Moesif API library for Ruby or Moesif Ruby API documentation
skip
Data type | Return type |
---|---|
Proc
|
Boolean |
Optional.
A Proc
that takes env
, headers
, and body
as arguments.
Returns a boolean. Return true
if you want to skip a particular event.
moesif_options['skip'] = Proc.new { |event, context, result|
# Add your custom code that returns true to skip logging the API call
if event.key?("rawPath")
# Skip probes to health page
event["rawPath"].include? "/health"
else
false
end
}
debug
Data type | Default |
---|---|
Boolean |
false
|
Optional.
If true
, the middleware prints out debug messages. In debug mode, the processing is not done in backend thread.
log_body
Data type | Default |
---|---|
Boolean |
true
|
Optional.
If false
, doesn’t log request and response body to Moesif.
Additional Methods for Updating User and Company Profiles
If you want to update User or Company profile using this SDK, use the following methods:
$moesif_middleware.update_user(user_profile)
$moesif_middleware.update_user_batch(user_profiles)
$moesif_middleware.update_company(company_profile)
$moesif_middleware.update_company_batch(company_profiles)
For information about the structure of profiles, see Moesif Ruby API documentation
Examples
See example_handler.rb
that contains an example Lambda handler using this middleware.
Bundling Your Gem Files
For AWS Lambda with Ruby, you have to bundle the gem dependencies into a ZIP file archive. For instructions, see Deploy Ruby Lambda functions with .zip file archives.
In the file where you define your Lambda handler, you may have to specify the location of the dependencies:
load_paths = Dir[
"./vendor/bundle/ruby/2.7.0/gems/**/lib"
]
$LOAD_PATH.unshift(*load_paths)
How to Get Help
If you face any issues using this middleware, try the troubheshooting guidelines. For further assistance, reach out to our support team.
Explore Other Integrations
Explore other integration options from Moesif: